package com.briup.web.servlet;

import com.briup.bean.Customer;
import com.briup.bean.ShopCar;
import com.briup.service.IShopCarService;
import com.briup.service.impl.ShopCarServiceIml;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "ShopCarDeleteServlet", value = "/shopCarDelete")
public class ShopCarDeleteServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        IShopCarService shopCarService = new ShopCarServiceIml();

        Integer bookId = Integer.valueOf(request.getParameter("bookId"));
        HttpSession session = request.getSession();
        Customer customer = (Customer) session.getAttribute("customer");
        Integer customerId = customer.getId();

        //调用对应的删除方法
        shopCarService.deleteShopCarByCidAndBId(customerId, bookId);

        //再查询一下新的购物车记录
        List<ShopCar> shopCarList = shopCarService.findShopCarsByCustomer(customerId);
        //如果查询出来的记录为空size（删除的是最后一条购物车记录）可以跳转到主页
        if (shopCarList.size() == 0) {
            response.sendRedirect("/index.jsp");
        } else {
            //覆盖原来的购物车session
            session.setAttribute("shopCarList", shopCarList);
            //跳转到购物车页面，重新渲染页面
            response.sendRedirect("/shopCar.jsp");
        }
    }

}
